    /* global visitUrl*/
    /* global getCookie */
    /* global template */
    /* global timeToTimestamp */
    (function ($) {
        //分类
        $.ajax({
            type: 'POST',
            url: "/antique/api/sysDict/query_list",
            data: {
                type: "specialtype",
                "token": getCookie('token')
            },
            dataType: "json",
            success: function (result) {
                if (result.code == 0) {
                    if (result.data && result.data.list && result.data.list.length > 0) {
                        var list = result.data.list;
                        for (var i = 0; i < list.length; i++) {
                            $("#firstCategory").append("<option value='" + list[i].id + "'>" + list[i].label + "</option>");
                        }
                    }
                }
            }
        });
    })(jQuery);
    $("#uploaderInput").on("change", function(e) {
        var file = e.target.files[0];
       
         /* global handlePic*/
         handlePic(file,2160,function(res){
                if (res.code == 0){
                    var html = template ('pho', res.data);
                    document.getElementById('uploaderFiles').innerHTML = html;
                } else {
                    $.toast(res.message, "cancel");
                }
                
          
        });
        
    });
    $("#content").keyup(function() {
        var value = $.trim(this.value);
        var descLength = $.trim(this.value).length;
        if (descLength <= 200) { 
            $("#descNumber").html(descLength);
        } else {
            $("#content").val(value.substring(0,200));
        }
    });

    $("#planEndDate").click(function () {
        $("#planEndDateSelecter").popup();
        $("#planEndDateSelecter").empty();
        //生成5天内的选择时间
        var timestamp = (new Date()).valueOf();
        var startDate = new Date(timestamp);
        //设置当前时间加5分钟,加10分钟,加30分钟
        var add5Minute = new Date(timestamp + 5 * 60 * 1000);
        var add10Minute = new Date(timestamp + 10 * 60 * 1000);
        var add30Minute = new Date(timestamp + 30 * 60 * 1000);
        //快速截拍5分钟，10分钟，30分钟
        $('<div></div>').addClass('title').text('快速截拍').appendTo('#planEndDateSelecter');
        var container = $('<div></div>').addClass('container').appendTo('#planEndDateSelecter');
        $('<div></div>').addClass('item').text('5分钟').attr({ 
            datetime : add5Minute.getFullYear() + "-" + prefixInteger(add5Minute.getMonth() + 1,2) + "-" + prefixInteger(add5Minute.getDate(),2) + " " + prefixInteger(add5Minute.getHours(),2) + ":" + prefixInteger(add5Minute.getMinutes(),2)  
        }).appendTo(container);
        $('<div></div>').addClass('item').text('10分钟').attr({ 
            datetime : add10Minute.getFullYear() + "-" + prefixInteger(add10Minute.getMonth() + 1,2) + "-" + prefixInteger(add10Minute.getDate(),2) + " " + prefixInteger(add10Minute.getHours(),2) + ":" + prefixInteger(add10Minute.getMinutes(),2)  
        }).appendTo(container);
        $('<div></div>').addClass('item').text('30分钟').attr({ 
            datetime : add30Minute.getFullYear() + "-" + prefixInteger(add30Minute.getMonth() + 1) + "-" + prefixInteger(add30Minute.getDate(),2) + " " + prefixInteger(add30Minute.getHours(),2) + ":" + prefixInteger(add30Minute.getMinutes(),2)  
        }).appendTo(container);
        $('<div></div>').addClass('auto-height').appendTo(container);

        var days = $('<div></div>').addClass('days').appendTo('#planEndDateSelecter');
        for (var i = 0; i < 10; i++) {
            var endDate = new Date(timestamp + i * 24 * 60 * 60 * 1000);
            var day = $('<div></div>').addClass('day').appendTo(days);
            $('<div></div>').addClass('title').text((endDate.getMonth() + 1) + '月' + (endDate.getDate()) + '日').appendTo(day);
            var _container = $('<div></div>').addClass('container').appendTo(day);
            for (var j = 1; j < 24; j++) {
                if ((i == 0) ? (startDate.getHours() < j && j > 9) : (j > 9)) {
                    $('<div></div>').addClass('item').attr({
                        datetime:endDate.getFullYear() + "-" + prefixInteger(endDate.getMonth() + 1,2) + "-" + prefixInteger(endDate.getDate(),2) + " " + prefixInteger(j,2) + ":00" 
                    }).click(function () {

                    }).text(prefixInteger(j,2) + ':00').appendTo(_container);
                }
            }
            $('<div></div>').addClass('auto-height').appendTo(_container);
        }
        $('#planEndDateSelecter .container .item').click(function(){
            var datetime = $(this).attr('datetime');
            $('#planEndDate').val(datetime);
            $('#planEndDateSelecter').hide();
        });
    });

    function sureBut(){
        var id = $("#ids").val();
        var name = $("#name").val();
        var content = $("#content").val();
        var endDate = $("#planEndDate").val();
        var category = $("#firstCategory").children('option:selected').val();
        // var image = $("#image").val();
        if (category == ''){
            $.toast("分类不能为空", "cancel");
            return;
        }
        if (name == ''){
            $.toast("名称不能为空", "cancel");
            return;
        }
        if (endDate == ''){
            $.toast("截拍日期不能为空", "cancel");
            return;
        }
        if (content == ''){
            $.toast("说明不能为空", "cancel");
            return;
        }
        // if (typeof image == 'undefined' || image == ''){
        //     $.toast("请上传图片", "cancel");
        //     return;
        // }

        var params = {};
        params.id = id;
        params.category = category;
        params.name = name;
        params.content = content;
        // params.image = image;
        endDate = endDate.replace(/\-/g,"/");
        params.endDate = new Date(endDate.toString()).getTime();
        params.token = getCookie('token');

        if (id){
            upPro(params);
           } else {
            addPro(params);
           }

    } 

    function addPro(params){
        delete params.id;
        $.ajax({
            type:'POST',
            url:"/antique/api/mallAuction/manage/add_auction",
            data: params,
            dataType:"json",
            success:function(res){
                if (res.code == 0){
                    /* global isMiniProgram wx */
                    $.toast("成功");
                    if ( isMiniProgram () ) {//小程序
                        wx.miniProgram.redirectTo({
                            url: "/pages/special_product?id=" + res.data.id
                        });
                    } else {
                        visitUrl("/special_product?id=" + res.data.id);
                    }
                 } else {
                    $.toast(res.message, "cancel");
                }
                
            }
        });
    }
    function upPro(params){
        $.ajax({
            type:'POST',
            url:"/antique/api/mallAuction/manage/update_auction",
            data: params,
            dataType:"json",
            success:function(res){
                if (res.code == 0){
                    if ( isMiniProgram () ) {//小程序
                        wx.miniProgram.redirectTo({
                            url: "/pages/special_manage"
                        });
                    } else {
                        visitUrl(document.referrer,true);
                    }
                    $.toast("成功");
                 } else {
                    $.toast(res.message, "cancel");
                }
                
            }
        });
    }

    function prefixInteger(num, n) {
        return (Array(n).join(0) + num).slice(-n);
    }
